//
//  YYVisitorView.swift
//  SinaStatuse（新浪微博）
//
//  Created by 李贤立 on 16/4/8.
//  Copyright © 2016年 李贤立. All rights reserved.
//

import UIKit
// 1.定义代理协议 YYVisitorViewDelegate
protocol YYVisitorViewDelegate : NSObjectProtocol {
    // 2.定义代理方法
    func visitorViewDidClickRegisterBtn(visitorView visitorView: UIView)
    
    func visitorViewDidClickLoginBtn(visitorView visitorView: UIView)
}

protocol ddd {
    func  dddd()
}


class YYVisitorView: UIView {
    // 3.定义代理属性
    weak var delegate:YYVisitorViewDelegate?
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 准备UI
        prepareUI()
        // 设置背景颜色
//        backgroundColor = UIColor(red: CGFloat(arc4random_uniform(255)/255), green: CGFloat(arc4random_uniform(255)/255), blue: CGFloat(arc4random_uniform(255)/255), alpha: 1)
//        backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 转轮动画
    func startRotation() {
        // 使用核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        // 动画完成不移除动画
        animation.removedOnCompletion = false
        // 添加动画到iconView
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    // 设置不同的访客视图的内容
    func setupVisitorViewInfo(imageName: String,message: String) {
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        
        // 隐藏小房子
        homeView.hidden = true
        
        // 隐藏遮盖
        coverView.hidden = true
        
        // 将一个view弄到最底部
        //        self.sendSubviewToBack(coverView)
        
        // 将一个view弄到最前面
        //        self.bringSubviewToFront(coverView)
    }
    // MARK: 准备UI
    private func prepareUI() {
        
        // 添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 禁用Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // 2.添加约束
        // 约束都往父控件添加
        /// item: 要约束的view
        /// attribute: 要约束的view的什么属性
        /// relatedBy: 参照关系
        /// toItem: 要参照的view
        /// attribute: 参照的view的什么属性
        
        // 将约束添加到父控件身上
        /// 转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        /// 遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        /// 房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        /// 消息label
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        /// 当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        /// 注册按钮
        /// 阿拉伯语言
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
    
//        // 转轮
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
//        // 遮盖
//        coverView.center = iconView.center
//        coverView.bounds.size = iconView.bounds.size
//        // 房子
//        homeView.center = iconView.center
//        // 消息
//        messageLabel.center.x = iconView.center.x
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        /// 当参照的view为nil时,attribute必须设置为NSLayoutAttribute.NotAnAttribute
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
    }
    // MARK: 懒加载控件
    // 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 消息文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(18)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        return label
    }()
    // 注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(YYVisitorView.didClickRegisterBtn), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // 登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(YYVisitorView.didClickLoginBtn), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    // MARK: 监听绑定事件
    // 4.调用代理方法
    @objc private func didClickRegisterBtn() {
        delegate?.visitorViewDidClickRegisterBtn(visitorView: self)

        
    }
    @objc private func didClickLoginBtn() {
        delegate?.visitorViewDidClickLoginBtn(visitorView: self)
    }
}
